package com.vcourse.service.edu.listener;

import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.vcourse.service.edu.entity.Subject;
import com.vcourse.service.edu.entity.excel.ExcelSubjectData;
import com.vcourse.service.edu.mapper.SubjectMapper;
import lombok.AllArgsConstructor;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author Zengsh123
 * @date 2020-06-02 13:26
 */
@Slf4j
@AllArgsConstructor //全参
@NoArgsConstructor //无参
public class ExcelSubjectDataListener extends AnalysisEventListener<ExcelSubjectData> {
    private SubjectMapper subjectMapper;

    /**
     * 每隔5条存储数据库，实际使用中可以3000条，然后清理list ，方便内存回收
     */
//    private static final int BATCH_COUNT = 5;
//    List<Subject> list = new ArrayList<Subject>();
//
//    private Map<String, String> levelOneId = new HashMap<>();

    /**
     *遍历每一行的记录
     */
    @Override
    public void invoke(ExcelSubjectData data, AnalysisContext context) {
        log.info("解析到一条记录: {}", data);
        //处理读取出来的数据
        //一级标题
        String levelOneTitle = data.getLevelOneTitle();
        //二级标题
        String levelTwoTitle = data.getLevelTwoTitle();
        log.info("levelOneTitle: {}", levelOneTitle);
        log.info("levelTwoTitle: {}", levelTwoTitle);

        // 组装数据：Subject
        // 1. 查看一级目录是否已存在
        Subject subjectOne = getByTitle(levelOneTitle);
        String parentId;
        if (subjectOne == null) {
            // 插入一级目录
            Subject levelOneSubject = new Subject();
            levelOneSubject.setTitle(levelOneTitle).setParentId("0").setSort(0);
            subjectMapper.insert(levelOneSubject);
            parentId = levelOneSubject.getId();
        } else {
            parentId = subjectOne.getId();
        }
        // 2. 操作二级目录
        Subject subjectTwo = getByTitle(levelTwoTitle, parentId);
        if (subjectTwo == null) {
            // 插入二级目录
            Subject levelTwoSubject = new Subject();
            levelTwoSubject.setTitle(levelTwoTitle).setParentId(parentId).setSort(0);
            subjectMapper.insert(levelTwoSubject);
        }

        // 存入数据库：subjectMapper.insert()
    }

    /**
     * 所有数据解析完成了 都会来调用
     */
    @Override
    public void doAfterAllAnalysed(AnalysisContext context) {
        log.info("所有数据解析完成！");
    }

    /**
     * 查询一级课程是否在
     */
    private Subject getByTitle(String title) {
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", title).eq("parent_id", "0");
        return subjectMapper.selectOne(queryWrapper);
    }

    /**
     * 查询二级课程是否在
     */
    private Subject getByTitle(String levelOne, String parentId) {
        QueryWrapper<Subject> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("title", levelOne).eq("parent_id", parentId);
        return subjectMapper.selectOne(queryWrapper);
    }
}
